/////////////////////////////////////////////////////////////////////////////
//
// XepdtMsgHandler.cpp
//
// Author : L.F.
// Date   : 2006.10.18
//
/////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "XepdtMsgHandler.h"

#include "XepdtClient.h"


/////////////////////////////////////////////////////////////////////////////
// CXepdtMessageHandler

const std::string CXepdtMessageHandler::typeChat    = std::string( "chat"    );

const std::string CXepdtMessageHandler::typeRecvRequest = std::string( "recv_request" );
const std::string CXepdtMessageHandler::typeRecvAccept  = std::string( "recv_accept"  );
const std::string CXepdtMessageHandler::typeRecvRejct   = std::string( "recv_rejct"   );
const std::string CXepdtMessageHandler::typeRecvUnknow  = std::string( "recv_unknow"  );

const std::string CXepdtMessageHandler::typeSendRequest = std::string( "send_request" );
const std::string CXepdtMessageHandler::typeSendAccept  = std::string( "send_accept"  );
const std::string CXepdtMessageHandler::typeSendReject  = std::string( "send_reject"  );
const std::string CXepdtMessageHandler::typeSendUnknow  = std::string( "send_unknow"  );

const std::string CXepdtMessageHandler::typeNetAddress  = std::string( "net_address"  );

void CXepdtMessageHandler::handleMessage( const Message& msg, MessageSession* session )
{
	/*
	ASSERT( m_pClient );
	m_pClient->OutputLogInfo( "CXepdtMessageHandler::handleMessage", CXepdtLogConsole::TEXT_RED );

	std::string jid = "";
	jid += stanza->from().username();
	jid += "@";
	jid += stanza->from().server();

	if ( stanza->subject() == CXepdtMessageHandler::typeChat )
	{
		OnChatSession( stanza->body(), jid );
		return;
	}

	*/
}

void CXepdtMessageHandler::OnChatSession( std::string chatContent, std::string jid )
{
	m_pClient->m_jidSession = jid;
	m_pClient->m_message    = chatContent;

	//AfxGetMainWnd()->SendMessage( \
	//						XepdtGlobal::WM_XEPDT_MESSAGE, \
	//						XepdtGlobal::WM_XEPDT_SESSION, \
	//						XepdtGlobal::SESSION_MSG );
}

/////////////////////////////////////////////////////////////////////////////
// CXepdtEventHandler method implementation

void CXepdtEventHandler::handleMessageEvent( const gloox::JID& from, gloox::MessageEventType event )
{
	ASSERT( m_pClient );
	m_pClient->OutputLogInfo( "CXepdtEventHandler::handleMessageEvent", CXepdtLogConsole::TEXT_RED );
}

void CXepdtChatStateHandler::handleChatState( const gloox::JID& from, gloox::ChatStateType state )
{
	ASSERT( m_pClient );
	m_pClient->OutputLogInfo( "CXepdtChatStateHandler::handleChatState", CXepdtLogConsole::TEXT_RED );

	std::string jid = "";
	jid += from.username();
	jid += "@";
	jid += from.server();

	m_pClient->m_jidSession = jid;

	//AfxGetMainWnd()->SendMessage( \
	//					XepdtGlobal::WM_XEPDT_MESSAGE, \
	//					XepdtGlobal::WM_XEPDT_SESSION, \
	//					XepdtGlobal::SESSION_CHATSTATE );
}

/////////////////////////////////////////////////////////////////////////////
// CXepdtSessionHandler constructor and destructor

CXepdtSessionHandler::CXepdtSessionHandler()
{
	m_pClient = NULL;
}

CXepdtSessionHandler::CXepdtSessionHandler( CXepdtClient* pClient )
{
	m_pClient = NULL;
	ASSERT( pClient != NULL );
	m_pClient = pClient;

	Cleanup();
}

CXepdtSessionHandler::~CXepdtSessionHandler()
{
	Cleanup();
}

/////////////////////////////////////////////////////////////////////////////
// CXepdtSessionHandler member method implementation

void CXepdtSessionHandler::handleMessageSession( gloox::MessageSession *session )
{
	ASSERT( m_pClient != NULL );

	m_pClient->m_jidSession = "";
	m_pClient->m_jidSession += session->target().username();
	m_pClient->m_jidSession += "@";
	m_pClient->m_jidSession += session->target().server();

	std::string jid = m_pClient->m_jidSession;

	// When and new message session opened
	// Create and new chat filter and handler
	// save the all information to the list...

	// m_pMessageSession = session;
	IT_MAP_SESSION it = m_mapSession.find( jid );
	if ( it == m_mapSession.end() )
	{
		m_mapSession[ jid ].pMessageSession = session;

		m_mapSession[ jid ].pXepdtMessageHandler = new CXepdtMessageHandler( m_pClient );
		m_mapSession[ jid ].pMessageSession->registerMessageHandler( m_mapSession[ jid ].pXepdtMessageHandler ); 

		m_mapSession[ jid ].pMessageEventFilter = new gloox::MessageEventFilter( session );
		m_mapSession[ jid ].pXepdtEventHandler  = new CXepdtEventHandler( m_pClient );
		m_mapSession[ jid ].pMessageEventFilter->registerMessageEventHandler( m_mapSession[ jid ].pXepdtEventHandler );

		m_mapSession[ jid ].pChatStateFilter = new gloox::ChatStateFilter( session );
		m_mapSession[ jid ].pXepdtChatStateHandler = new CXepdtChatStateHandler( m_pClient );
		m_mapSession[ jid ].pChatStateFilter->registerChatStateHandler(  m_mapSession[ jid ].pXepdtChatStateHandler );
	}

	//AfxGetMainWnd()->PostMessage( \
	//					XepdtGlobal::WM_XEPDT_MESSAGE, \
	//					XepdtGlobal::WM_XEPDT_SESSION, \
	//					XepdtGlobal::SESSION_OPEN );
}

void CXepdtSessionHandler::Cleanup()
{
	for ( IT_MAP_SESSION it = m_mapSession.begin(); it != m_mapSession.end(); it++ )
	{
		( *it ).second.pChatStateFilter->removeChatStateHandler();
		delete ( *it ).second.pChatStateFilter;

		( *it ).second.pMessageEventFilter->removeMessageEventHandler();
		delete ( *it ).second.pMessageEventFilter;

		( *it ).second.pMessageSession->removeMessageHandler();
		delete ( *it ).second.pMessageSession;

		delete ( *it ).second.pXepdtEventHandler;
		delete ( *it ).second.pXepdtChatStateHandler;
		delete ( *it ).second.pXepdtMessageHandler;
	}

	m_mapSession.clear();
}

void CXepdtSessionHandler::SendChatMessage( std::string& jid, std::string& msg )
{
	IT_MAP_SESSION it = m_mapSession.find( jid );
	if ( it == m_mapSession.end() )
	{
		gloox::MessageSession *session = NULL;
		gloox::JID j( jid );
		j.setResource( "Xepdt" );

		session = new gloox::MessageSession( m_pClient, j );
		ASSERT( session );

		m_mapSession[ jid ].pMessageSession = session;

		m_mapSession[ jid ].pXepdtMessageHandler = new CXepdtMessageHandler( m_pClient );
		m_mapSession[ jid ].pMessageSession->registerMessageHandler( m_mapSession[ jid ].pXepdtMessageHandler ); 

		m_mapSession[ jid ].pMessageEventFilter = new gloox::MessageEventFilter( session );
		m_mapSession[ jid ].pXepdtEventHandler  = new CXepdtEventHandler( m_pClient );
		m_mapSession[ jid ].pMessageEventFilter->registerMessageEventHandler( m_mapSession[ jid ].pXepdtEventHandler );

		m_mapSession[ jid ].pChatStateFilter = new gloox::ChatStateFilter( session );
		m_mapSession[ jid ].pXepdtChatStateHandler = new CXepdtChatStateHandler( m_pClient );
		m_mapSession[ jid ].pChatStateFilter->registerChatStateHandler(  m_mapSession[ jid ].pXepdtChatStateHandler );
	}

	if ( m_mapSession[ jid ].pMessageSession != NULL )
	{
		m_mapSession[ jid ].pMessageEventFilter->raiseMessageEvent( gloox::MessageEventDisplayed );
		m_mapSession[ jid ].pMessageEventFilter->raiseMessageEvent( gloox::MessageEventComposing );
		m_mapSession[ jid ].pChatStateFilter->setChatState( gloox::ChatStateComposing );
		m_mapSession[ jid ].pMessageSession->send( msg, CXepdtMessageHandler::typeChat );
	}
}

gloox::MessageSession* CXepdtSessionHandler::GetMessageSession( std::string& jid, std::string& res )
{
	IT_MAP_SESSION it = m_mapSession.find( jid );
	if ( it == m_mapSession.end() )
	{
		gloox::MessageSession *session = NULL;
		gloox::JID j( jid );
		j.setResource( res );

		session = new gloox::MessageSession( m_pClient, j );
		ASSERT( session );

		m_mapSession[ jid ].pMessageSession = session;

		m_mapSession[ jid ].pXepdtMessageHandler = new CXepdtMessageHandler( m_pClient );
		m_mapSession[ jid ].pMessageSession->registerMessageHandler( m_mapSession[ jid ].pXepdtMessageHandler ); 

		m_mapSession[ jid ].pMessageEventFilter = new gloox::MessageEventFilter( session );
		m_mapSession[ jid ].pXepdtEventHandler  = new CXepdtEventHandler( m_pClient );
		m_mapSession[ jid ].pMessageEventFilter->registerMessageEventHandler( m_mapSession[ jid ].pXepdtEventHandler );

		m_mapSession[ jid ].pChatStateFilter = new gloox::ChatStateFilter( session );
		m_mapSession[ jid ].pXepdtChatStateHandler = new CXepdtChatStateHandler( m_pClient );
		m_mapSession[ jid ].pChatStateFilter->registerChatStateHandler(  m_mapSession[ jid ].pXepdtChatStateHandler );
	}

	return m_mapSession[ jid ].pMessageSession;
}